import router from "@/router";
import { useBreakCrumb } from "@/store/breakCrumb";
import * as api from "@/api/permission";
import { usePermission } from "@/store/permission";

//前置守卫（钩子）
router.beforeEach(async (to, from) => {
  let permissStore = usePermission();

  // console.log(permissStore.menulist);

  // console.log("beforeEach-to:", to);
  let token = sessionStorage.getItem("qf-token");
  //判断本地的sessionStorage中是否有token，如果有，啥也不干
  //如果没有，访问的不是登录页，强行跳转到登入页，访问的是登录页，直接放行
  if (!token) {
    if (to.path !== "/login") {
      return {
        path: "/login",
      };
    }
  } else {
    if (permissStore.menulist.length === 0) {
      await permissStore.fetch_menulist();
      return {
        ...to,
      };
    }
    if (to.path === "/login") {
      return {
        path: "/",
      };
    }
  }
});
//全局后置钩子

router.afterEach((to, from) => {
  // console.log(to);
  let breakCrumbStore = useBreakCrumb();
  breakCrumbStore.$patch((state) => {
    state.datas = to.matched;
  });
});
